﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iLangl.Model;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common.Utilities;

namespace iLangl.Customer.DNN
{
	public class LanguageManager
	{
		#region Singelton Pattern
		private static LanguageManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the ProjectManager.
		/// </summary>
		public static LanguageManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new LanguageManager();

						}
					}
				}
				return instance;
			}
		}
		#endregion

		public Locale GetLanguage(String langugeCode)
		{
			return LocaleController.Instance.GetLocale(langugeCode);
		}

		public static Language LocaleToLanguage(Locale locale)
		{
			Language result = new Language();
			result.LanguageId = locale.Code;
			result.LanguageName = locale.Text;

			return result;
		}

		public IList<Language> GetPortalLanguages(int portalId)
		{
			IList<Language> result = new List<Language>();

			foreach (Locale language in LocaleController.Instance.GetLocales(portalId).Values)
			{
				result.Add(LocaleToLanguage(language));
			}

			return result;
		}
	}
}